import json,redis

from models.user import UserModel
from sqlalchemy.orm import load_only
from common.utils.login_utils import login_required
import random
class BaseCacheTTL(object):
    TTL = 0
    MAX_DELTA = 10 *60
    @classmethod
    def get_val(cls):
        return cls.TTL + random.randrange(0,cls.MAX_DELTA)
class UserProfileCacheTTL(BaseCacheTTL):
    TTL = 30*60*60

class UserCache:

    def __init__(self,user_id):
        self.key = 'user:{}:frofile'.format(user_id)
        self.user_id = user_id
    def get(self):
        rds = redis.Redis()
        try:
            ret = rds.get(self.key)
        except Exception as e:
            print("...",e)
            ret = None
        if ret is not None:
            if ret == b'1':
                return None
            else:
                user_dict = json.loads(ret)
                print('缓存返回',user_dict)
                return user_dict
        else:
            try:
                user = UserModel.query.options(load_only(
                    UserModel.account,
                    UserModel.mobile,
                    UserModel.uname,
                    UserModel.password,
                    UserModel.gender,
                    UserModel.profile_photo,
                    UserModel.created_time,
                    UserModel.email,
                    UserModel.is_media,
                    UserModel.is_verified,
                    UserModel.introduction,
                    UserModel.certificate,
                    UserModel.article_count,
                    UserModel.following_count,
                    UserModel.fans_count,
                    UserModel.like_count,
                    UserModel.read_count,
                    UserModel.status,
                    UserModel.superuser
                )).filter_by(uid=self.user_id).first()
            except Exception as e:
                print(",,,,",e)
                raise e
            if user is None:
                return None
            else:
                user_dict = {
                    'account':user.account,
                    'mobile':user.mobile,
                    'uname':user.uname,
                    'password':user.password,
                    'gender':user.gender,
                    'profile_photo':user.profile_photo,
                    'email':user.email,
                    'created_time':user.created_time,
                    'is_media':user.is_media,
                    'is_verified':user.is_verified,
                    'introduction':user.introduction,
                    'certificate':user.certificate,
                    'article_count':user.article_count,
                    'following_count':user.following_count,
                    'fans_count':user.fans_count,
                    'like_count':user.like_count,
                    'read_count':user.read_count,
                    'status':user.status,
                    'superuser':user.superuser
                }
                rds.set(self.key,json.dumps(user_dict),UserProfileCacheTTL.get_val())
                print('s数据库缓存返回',user_dict)
                return user_dict


